///////////////////////////////////////////////////////////////////////////
// Copyright 2009 Arabic Game Developer Network, All rights reserved
// www.agd-online.com - info@agdn-online.com
// Based on code originally authored by Salwan Alhilali for the kvalley2 project
// Copyright 2008, In|Framez Co., All rights reserved
//
// File: input.cpp
// Description: Input system
//
///////////////////////////////////////////////////////////////////////////

// This file is part of kvalleyX.
// 
// kvalleyX is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// kvalleyX is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with kvalleyX.  If not, see <http://www.gnu.org/licenses/>.


/////////////////// #includes ///////////////////
#include "input.h"

namespace CoreLib
{
/////////////////// Functions Implementations /////////////////

/////////////////// InputManager Class Implementation ///////////////////
// InputManager* InputManager::Instance = NULL;

InputManager::InputManager() :
	m_bJoystickReady(false),
	m_cInjectedChar(0)
{
// 	Instance = this;
}

InputManager::~InputManager()
{
// 	TerminateDirectInput();
// 	Instance = NULL;
}



//////////////////////////////////////////////////////////////////////////
// Function Name : InputManager::KeyboardAssignControl
//
// Purpose : Assigns the given directinput keycode to the game control.
//
// Params : 
//    int eGameControl : Game control id
//    int eDIKey : DirectInput key code
//
// Return Value : None.
//////////////////////////////////////////////////////////////////////////
void InputManager::KeyboardAssignControl(const int control,
										 InputKeyCode keycode)
{
	m_KeyboardActionMap.insert(std::pair<int, int>(control, 
											  m_KeyCodeMap[keycode]));
	m_KeyboardActionEvents.insert(std::pair<int, InputEvent>(control, 
														InputEvent_KeyUp));
}

//////////////////////////////////////////////////////////////////////////
// Function Name : InputManager::KeyboardRemoveControl
//
// Purpose : Removes the given control and all it's assigned keys.
//
// Params : 
//    int eGameControl : Game control id
//
// Return Value : None.
//////////////////////////////////////////////////////////////////////////
void InputManager::KeyboardRemoveControl(const int eGameControl)
{
// 	m_KeyboardActionMap.erase(eGameControl);
// 	m_KeyboardActionEvents.erase(eGameControl);
}

//////////////////////////////////////////////////////////////////////////
// Function Name : InputManager::JoystickButtonAssignControl
//
// Purpose : Assigns a game control to a joystick button from 0-31
//			 (up/down/left/right are special cases of buttons).
//
// Params : 
//	  int eGameControl : Game control id
//	  int iButton : Button code (0-31) or (JoystickAxis_Up/Down/Left/Right)
//
// Return Value : None.
//////////////////////////////////////////////////////////////////////////
void InputManager::JoystickButtonAssignControl(const int eGameControl, const int iButton)
{
// 	if(!m_bJoystickReady)
// 		return;
// 	m_JoystickActionMap.insert(pair<int, int>(eGameControl, iButton));
// 	m_JoystickActionEvents.insert(pair<int, InputEvent>(eGameControl, 
// 														InputEvent_KeyUp));
}

//////////////////////////////////////////////////////////////////////////
// Function Name : InputManager::JoystickUpAssignControl
//
// Purpose : Assigns a game control to the y+ axis and POV-up.
//
// Params : 
//	  int eGameControl : Game control id
//
// Return Value : None.
//////////////////////////////////////////////////////////////////////////
void InputManager::JoystickUpAssignControl(const int eGameControl)
{
/*	if(!m_bJoystickReady)
		return;
	JoystickButtonAssignControl(eGameControl, JoystickAxis_Up);*/
}

//////////////////////////////////////////////////////////////////////////
// Function Name : InputManager::JoystickDownAssignControl
//
// Purpose : Assigns a game control to the y- axis and POV-down.
//
// Params : 
//	  int eGameControl : Game control id
//
// Return Value : None.
//////////////////////////////////////////////////////////////////////////
void InputManager::JoystickDownAssignControl(int eGameControl)
{
// 	if(!m_bJoystickReady)
// 		return;
// 	JoystickButtonAssignControl(eGameControl, JoystickAxis_Down);
}

//////////////////////////////////////////////////////////////////////////
// Function Name : InputManager::JoystickRightAssignControl
//
// Purpose : Assigns a game control to the x+ axis and POV-right.
//
// Params : 
//	  int eGameControl : Game control id
//
// Return Value : None.
//////////////////////////////////////////////////////////////////////////
void InputManager::JoystickRightAssignControl(const int eGameControl)
{
// 	if(!m_bJoystickReady)
// 		return;
// 	JoystickButtonAssignControl(eGameControl, JoystickAxis_Right);
}

//////////////////////////////////////////////////////////////////////////
// Function Name : InputManager::JoystickLeftAssignControl
//
// Purpose : Assigns a game control to the x- axis and POV-left.
//
// Params : 
//	  int eGameControl : Game control id
//
// Return Value : None.
//////////////////////////////////////////////////////////////////////////
void InputManager::JoystickLeftAssignControl(const int eGameControl)
{
// 	if(!m_bJoystickReady)
// 		return;
// 	JoystickButtonAssignControl(eGameControl, JoystickAxis_Left);
}

//////////////////////////////////////////////////////////////////////////
// Function Name : InputManager::JoystickButtonRemoveControl
//
// Purpose : Removes the given control and all it's assigned buttons.
//
// Params : 
//	  int eGameControl : Game control id
//
// Return Value : None.
//////////////////////////////////////////////////////////////////////////
void InputManager::JoystickButtonRemoveControl(const int eGameControl)
{
// 	if(!m_bJoystickReady)
// 		return;
// 	m_JoystickActionMap.erase(eGameControl);
// 	m_JoystickActionEvents.erase(eGameControl);
}

//////////////////////////////////////////////////////////////////////////
// Function Name : InputManager::RemoveAllControls
//
// Purpose : Clears the action map, and action events.
//
// Params : None.
//
// Return Value : None.
//////////////////////////////////////////////////////////////////////////
void InputManager::RemoveAllControls(void)
{
// 	m_KeyboardActionMap.clear();
// 	m_KeyboardActionEvents.clear();
// 	if(!m_bJoystickReady)
// 		return;
// 	m_JoystickActionMap.clear();
// 	m_JoystickActionEvents.clear();
}

//////////////////////////////////////////////////////////////////////////
// Function Name : InputManager::ControlDown
//
// Purpose : Retrieves the game control state.
//
// Params : 
//    int eGameControl : Game control id
//
// Return Value : 'true' if the control is down, 'false' otherwise.
//////////////////////////////////////////////////////////////////////////
const bool InputManager::ControlDown(const int eGameControl)
{
// 	bool kb = m_KeyboardActionEvents[eGameControl] == InputEvent_KeyDown? 
// 	true : false;
// 	bool js = m_JoystickActionEvents[eGameControl] == InputEvent_KeyDown? 
// 	true : false;
// 	return kb | js;
}

//////////////////////////////////////////////////////////////////////////
// Function Name : InputManager::GetPressedCharacter
//
// Purpose : Returns the last pressed character code which is obtained 
//			 via the message procedure so it respects local user settings,
//			 and repeat frequency.
//			 It also removes the injected character.
//
// Params : None.
//
// Return Value : The character code from (A-Z, a-z, 0-9)
//			      '0' if no characters were pressed.
//////////////////////////////////////////////////////////////////////////
const char InputManager::GetPressedCharacter(void)
{ 
// 	char cInj = m_cInjectedChar; 
// 	m_cInjectedChar = 0; 
// 	return cInj; 
}

//////////////////////////////////////////////////////////////////////////
// Function Name : InputManager::_injectCharacter
//
// Purpose : Injects character obtained via WM_CHAR message, only if its
//			 from (A-Z),(a-z), or (0-9)
//
// Params : 
//	  char cCharacterCode : character code
//
// Return Value : None.
//////////////////////////////////////////////////////////////////////////
void InputManager::_injectCharacter(const char cCharacterCode)
{
// 	m_cInjectedChar = 0;
// 	if( (cCharacterCode >= 'A' && cCharacterCode <= 'Z') ||
// 		(cCharacterCode >= 'a' && cCharacterCode <= 'z') ||
// 		(cCharacterCode >= '0' && cCharacterCode <= '9') )
// 	{
// 		m_cInjectedChar = cCharacterCode;
// 	}
}

}// CoreLib namespace